package com.hooper.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hooper.cmn.client.DictClient;
import com.hooper.hosp.enums.DictEnum;
import com.hooper.hosp.model.user.Patient;
import com.hooper.hospital.common.exception.HospitalException;
import com.hooper.hospital.common.result.ResultCodeEnum;
import com.hooper.hospital.common.utils.AuthContextHolder;
import com.hooper.user.mapper.PatientMapper;
import com.hooper.user.service.PatientService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/10/07/19:02
 */
@RequiredArgsConstructor
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    private final DictClient dictClient;

    @Override
    public List<Patient> listByUserId(String userId) {

        if (StringUtils.isEmpty(userId)) {
            throw new HospitalException(ResultCodeEnum.LOGIN_REQUIRE);
        }
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getUserId, userId);
        return baseMapper.selectList(wrapper).stream().peek(patient -> {
            //根据证件类型编码，获取证件类型具体指
            String certificatesTypeString =
                    dictClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
            //联系人证件类型
            String contactsCertificatesTypeString =
                    dictClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
            //省
            String provinceString = dictClient.getLocation(patient.getProvinceCode());
            //市
            String cityString = dictClient.getLocation(patient.getCityCode());
            //区
            String districtString = dictClient.getLocation(patient.getDistrictCode());
            patient.getParam().put("certificatesTypeString", certificatesTypeString);
            patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
            patient.getParam().put("provinceString", provinceString);
            patient.getParam().put("cityString", cityString);
            patient.getParam().put("districtString", districtString);
            patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        }).collect(Collectors.toList());
    }
}
